﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DlrBasic.Runtime
{
    /// <summary>
    /// Utility class for BASIC comparison operations. Instead of 
    /// normal boolean results returns -1 or 0.
    /// </summary>
    public static class DlrBasicCompare
    {
        public const short True = -1;
        public const short False = 0;

        public static short CompareInt(RelationalOperator op, short value1, short value2)
        {
            int result = value1 - value2;
            return DoCompare(op, result);
        }

        public static short CompareSingle(RelationalOperator op, float value1, float value2)
        {
            int result = value1 == value2 ? 0 : (value1 < value2 ? -1 : 1);
            return DoCompare(op, result);
        }

        public static short CompareDouble(RelationalOperator op, double value1, double value2)
        {
            int result = value1 == value2 ? 0 : (value1 < value2 ? -1 : 1);
            return DoCompare(op, result);
        }

        public static short CompareString(RelationalOperator op, string value1, string value2)
        {
            int result = StringComparer.InvariantCulture.Compare(value1, value2);
            return DoCompare(op, result);
        }

        private static short DoCompare(RelationalOperator op, int result)
        {
            switch (op)
            {
                case RelationalOperator.Equality:
                    return result == 0 ? True : False;
                case RelationalOperator.Inequality:
                    return result != 0 ? True : False;
                case RelationalOperator.LessThan:
                    return result < 0 ? True : False;
                case RelationalOperator.GreaterThan:
                    return result > 0 ? True : False;
                case RelationalOperator.LessThanOrEqualTo:
                    return result <= 0 ? True : False;
                case RelationalOperator.GreaterThanOrEqualTo:
                    return result >= 0 ? True : False;
            }
            throw new NotImplementedException();
        }
    }
    
    /// <summary>
    /// Relational operation types.
    /// </summary>
    public enum RelationalOperator
    {
        Equality,
        Inequality,
        LessThan,
        GreaterThan,
        LessThanOrEqualTo,
        GreaterThanOrEqualTo,
    }
}
